package org.greatstorm.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.expressme.webwind.Mapping;
import org.expressme.webwind.renderer.Renderer;
import org.expressme.webwind.renderer.TemplateRenderer;
import org.greatstorm.entity.Article;
import org.greatstorm.entity.Category;
import org.greatstorm.entity.EntityObject;
import org.greatstorm.entity.Tag;
import org.greatstorm.service.ArticleCategoryMediator;
import org.greatstorm.util.GlobalVariables;
import com.google.inject.Guice;

public class ArticleAction extends GeneralAction {
	private ArticleCategoryMediator artcatMediator;

	public ArticleAction() {
		artcatMediator = Guice.createInjector().getInstance(
				ArticleCategoryMediator.class);
	}

	@Mapping("/admin")
	public Renderer showArticlePage() {
		Map<String, Object> model = new HashMap<String, Object>();

		renderHeader(model);
		renderLeftBar(model);

		return new TemplateRenderer("/admin_main.vm", model);
	}

	private void renderLeftBar(Map<String, Object> model) {
		Map<String, String> categoryMap = artcatMediator.getCategroyMap();
		// total article count
		long total = artcatMediator.getArticleCnt();

		model.put("allcategory", categoryMap);
		model.put("total_article", total);
	}

	private void renderHeader(Map<String, Object> model) {
		model.put("admin_link", GlobalVariables.ADMIN_URL);
	}

	@Mapping("/admin/categories/$1")
	public Renderer showArticleByCategory(String key) {
		Map<String, Object> model = new HashMap<String, Object>();

		renderHeader(model);
		renderLeftBar(model);

		// render the article list
		renderArticleList(model, key);

		return new TemplateRenderer("/admin_article.vm", model);
	}

	@Mapping("/admin/blogs")
	public Renderer showAllArticle() {
		Map<String, Object> model = new HashMap<String, Object>();

		renderHeader(model);
		renderLeftBar(model);

		// render the article list
		renderArticleList(model);

		return new TemplateRenderer("/admin_article.vm", model);
	}

	@Mapping("/admin/blogs/new")
	public Renderer newArticle() {
		Map<String, Object> model = new HashMap<String, Object>();

		renderHeader(model);
		renderLeftBar(model);
		renderCategoryList(model);

		return new TemplateRenderer("/admin_new_article.vm", model);
	}

	@Mapping("/admin/blogs/add")
	public String addNewArticle() {
		String title = this.getRequest().getParameter("blog[title]");
		String categoryKey = this.getRequest()
				.getParameter("blog[category_id]");
		String tagString = this.getRequest().getParameter("blog[tag]");
		String[] tags = null;
		if (tagString == null) {
			tags = new String[0];
		} else {
			tags = tagString.split(",");
		}

		List<String> tagList = new ArrayList<String>();
		for (String t : tags) {
			tagList.add(t);
		}

		String content = this.getRequest().getParameter("articleContent");

		artcatMediator.addNewArticle(categoryKey, title, content, tagList);

		return "redirect:/admin/blogs";
	}

	private void renderCategoryList(Map<String, Object> model) {
		Map<String, String> categoryMap = artcatMediator
				.getCategroyMapWithoutCount();
		model.put("allcatergoryname", categoryMap);
	}

	private void renderArticleList(Map<String, Object> model) {
		String pageStr = this.getRequest().getParameter("page");
		if (pageStr == null) {
			pageStr = "1";
		}

		Map<String, List<String>> articleMap = artcatMediator
				.getAllArticleMap(Integer.valueOf(pageStr));

		model.put("articleList", articleMap);
		model.put("category_name", "全部博客");
	}

	private void renderArticleList(Map<String, Object> model, String key) {
		String pageStr = this.getRequest().getParameter("page");
		if (pageStr == null) {
			pageStr = "1";
		}
		Map<String, List<String>> articleMap = artcatMediator
				.getArticleMapByCategory(Integer.valueOf(pageStr), key);

		// TODO: add page control render

		model.put("articleList", articleMap);

		String categoryName = artcatMediator.getCategoryName(key);
		model.put("category_name", categoryName);
	}

	@Mapping("/admin/blogs/delete")
	public String deleteArticle() {
		String articleTitile = artcatMediator.deleteArticle(this.getRequest()
				.getParameter("p"));
		return articleTitile + "已被成功删除";
	}

	@Mapping("/admin/blogs/sticky")
	public String stickyArticle() {
		String opt = this.getRequest().getParameter("opt");
		int optVal = 0;
		if (opt.equals("0")) {
			optVal = 0;
		} else {
			optVal = 1;
		}
		artcatMediator.stickyArticle(this.getRequest().getParameter("p"),
				optVal);
		return "";
	}

	@Mapping("/admin/blogs/lock")
	public String lockArticle() {
		String opt = this.getRequest().getParameter("opt");
		int optVal = 0;
		if (opt.equals("0")) {
			optVal = 0;
		} else {
			optVal = 1;
		}

		artcatMediator.stickyArticle(this.getRequest().getParameter("p"),
				optVal);
		return "";
	}

	@Mapping("/admin/blogs/edit")
	public Renderer editArticle() {
		Map<String, Object> model = new HashMap<String, Object>();

		renderHeader(model);
		renderLeftBar(model);
		renderCategoryList(model);
		renderArticleContent(model);

		return new TemplateRenderer("/admin_edit_article.vm", model);
	}

	@SuppressWarnings("unchecked")
	private void renderArticleContent(Map<String, Object> model) {
		Map<String, Object> ret = artcatMediator.getArticleMapByKey(this
				.getRequest().getParameter("p"));

		EntityObject category = (EntityObject) ret.get(Category.KIND);
		model.put("articlecatkey", category.getKey());

		EntityObject article = (EntityObject) ret.get(Article.KIND);
		model.put("articletitle", article.get(Article.ARTICLE_TITLE));

		List<EntityObject> tags = (List<EntityObject>) ret.get(Tag.KIND);
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < tags.size(); i++) {
			EntityObject o = tags.get(i);
			sb.append(o.get(Tag.TAG_NAME));
			if (i != tags.size() - 1) {
				sb.append(",");
			}
		}

		model.put("taglist", sb.toString());
		model.put("articlekey", article.getKey());
		model.put("articlecontent", article.get(Article.CONTENT));

	}

	@Mapping("/admin/blogs/saveedit")
	public String saveEditResult() {
		String articleKey = this.getRequest().getParameter("blog[key]");
		String title = this.getRequest().getParameter("blog[title]");
		String categoryKey = this.getRequest()
				.getParameter("blog[category_id]");
		String tagString = this.getRequest().getParameter("blog[tag]");
		String[] tags = null;
		if (tagString == null) {
			tags = new String[0];
		} else {
			tags = tagString.split(",");
		}

		List<String> tagList = new ArrayList<String>();
		for (String t : tags) {
			tagList.add(t);
		}

		String content = this.getRequest().getParameter("articleContent");

		artcatMediator.editArticle(articleKey, categoryKey, title, content, tagList);

		return "redirect:/admin/blogs";
	}
}
